#!/bin/bash

echo "🚀 Cursor 性能优化脚本"
echo "========================"

# 清理缓存文件
echo "🧹 清理缓存文件..."
rm -rf .expo/
rm -rf .metro-cache/
rm -rf node_modules/.cache/
rm -rf ios/build/
rm -rf android/build/
rm -rf android/app/build/
rm -rf android/.gradle/
rm -f *.tsbuildinfo
rm -f .eslintcache
rm -f .prettiercache

# 清理临时文件
echo "🗑️  清理临时文件..."
find . -name "*.tmp" -delete
find . -name "*.temp" -delete
find . -name ".DS_Store" -delete

# 重新安装依赖（如果需要）
echo "📦 检查依赖..."
if [ ! -d "node_modules" ] || [ "$1" = "--reinstall" ]; then
    echo "重新安装依赖..."
    rm -rf node_modules
    yarn install
fi

# 优化 TypeScript 配置
echo "⚙️  优化 TypeScript 配置..."
if [ -f "tsconfig.json" ]; then
    # 备份原配置
    cp tsconfig.json tsconfig.json.bak
    echo "TypeScript 配置已备份"
fi

# 创建性能优化的 tsconfig
cat > tsconfig.json << 'EOF'
{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "incremental": true,
    "tsBuildInfoFile": "./node_modules/.cache/.tsbuildinfo"
  },
  "include": [
    "**/*.ts",
    "**/*.tsx",
    "**/*.js",
    "**/*.jsx"
  ],
  "exclude": [
    "node_modules",
    "ios",
    "android",
    "build",
    "dist",
    ".expo",
    "*.tsbuildinfo"
  ]
}
EOF

echo "✅ 性能优化完成！"
echo ""
echo "💡 建议："
echo "1. 重启 Cursor 编辑器"
echo "2. 如果还是很慢，可以尝试："
echo "   - 关闭不必要的扩展"
echo "   - 减少打开的文件数量"
echo "   - 使用工作区而不是单个文件夹"
echo ""
echo "🔧 其他优化选项："
echo "   ./optimize-performance.sh --reinstall  # 重新安装依赖" 